package video.com.controller;


import com.Exception.GlobalException;
import com.feign.CommonFeignClient;
import com.responseData.R;
import com.responseData.ResponseData;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import video.com.entity.UserBehavior;
import video.com.transportEntity.vo.Page;
import video.com.service.VideoFileService;
import video.com.service.VideoService;
import video.com.transportEntity.vo.FileUploadVo;
import video.com.transportEntity.vo.VideoListPage;
import video.com.utils.RedisUtils;


import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xwj
 * @since 2023-11-11
 */
@Slf4j
@RestController
@RequestMapping("/video")
public class VideoController {

    @Autowired
    private VideoService videoService;

    @Autowired
    private VideoFileService videoFileService;

    /**
     * 视频文件上传能接口
     * @param file
     * @param fileUploadVo
     * @return
     */
    @PostMapping("/uploadVideo")
    public R uploadVideo(@RequestParam("file") MultipartFile file , FileUploadVo fileUploadVo){
        Object result ;
        try{
            result = videoFileService.uploadVideo(file,fileUploadVo);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.error(e.getCause());
        }

        return ResponseData.success(result);
    }

    /**
     * 视频文件封面上传接口
     * @param file
     * @param fileUploadVo
     * @return
     */
    @PostMapping("/uploadVideoCover")
    public R uploadVideoCover(MultipartFile file , FileUploadVo fileUploadVo){
        Object result ;
        try{
            result = videoFileService.uploadVideoCover(file,fileUploadVo);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.error(e.getCause());
        }

        return ResponseData.success(result);
    }

    /**
     * 通过文件加密值得到文件是否以上传哪几块
     * @param fileHash
     * @return
     */
    @GetMapping("/hasUploadChunks/{fileHash}")
    public R hasUploadChunks(@PathVariable String fileHash){
        Set set = null;
        try{
            set = RedisUtils.redis.opsForSet().members(fileHash);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseData.error("失败");
        }
        return ResponseData.success(set);
    }

    /**
     * 上传视频相关信息
     * @param info 视频信息
     */
    @PostMapping("/uploadVideoInfo")
    public R uploadVideoInfo(@RequestBody Map<String, Object> info){
        try{
            videoService.uploadVideoInfo(info);
        }catch (Exception e){
            e.printStackTrace();
            if(e instanceof GlobalException){
                return ResponseData.error(e.getMessage());
            }else{
                return ResponseData.error("上传失败");
            }
        }

        return ResponseData.success("");
    }

    /**
     *根据videoId获取视频信息
     * @return
     * @param uid
     * @return
     */
    @GetMapping("/getVideoInfo/{uid}")
    public R getVideoInfo(@PathVariable String uid){
        Map<String, Object> videoInfo = new HashMap<>();
        try{
            videoInfo = videoService.getVideoInfo(uid);
        }catch (Exception e){
            e.printStackTrace();
            log.info(e.getMessage());
            return ResponseData.error("获取视频信息失败");
        }

        return ResponseData.success(videoInfo);
    }

    /**
     * 根据用户推荐视频
     * @param params
     * @return
     */
    @PostMapping("/userCommend")
    public R userCommend(@RequestBody VideoListPage params){

        List<Map<String, Object>> info = new ArrayList<>(20);

        try{
            info = videoService.userRecommend(params);
        }catch (Exception e){
            e.printStackTrace();
            log.warn(e.getMessage());
            return ResponseData.error("数据获取异常");
        }
        return ResponseData.success(info);
    }

    /**
     * 根据视频推荐相关视频
     * @param page
     * @return
     */
    @PostMapping("/videoCommend")
    public R videoCommend(@RequestBody VideoListPage page){

        List<Map<String, Object>> info = new ArrayList<>(20);

        try{
            info = videoService.videoRecommend(page);
        }catch (Exception e){
            e.printStackTrace();
            log.info(e.getMessage());
            return ResponseData.error(e.getCause());
        }
        return ResponseData.success(info);
    }

    /**
     * 更新视频的信息 ，如观看数 ，弹幕数 的记录 必须携带用户信息才可进行
     * @param info
     */
    @PostMapping("/updateVideoInfo")
    public void updateVideoInfo(@RequestBody Map<String, Object> info){
        try{
            videoService.updateVideoInfoWithUserId(info);
        }catch (Exception e){
            log.error("异常信息: {}",e.getCause());

        }
    }

    /**
     * 未登陆人员观看时 记录视频观看数
     * @param info
     */
    @PostMapping("/tourist")
    public void tourist(@RequestBody Map<String, Object> info){
        try{
            videoService.tourist(info);
        }catch (Exception e){
            log.error("异常信息: {}",e.getCause());

        }
    }

    /**
     * 通过视频id获取当前用户对视频的动作
     * @param vid 视频Id
     * @return
     */
    @GetMapping("/userBehavior/{vid}")
    public R userBehaviorByVid(@PathVariable String vid){
        Map<String, Object> userBehavior = new HashMap<>();
        try{
            userBehavior = videoService.userBehaviorByVid(vid);
        }catch (Exception e){
            log.error("异常信息: {}",e.getCause());
            return ResponseData.error("信息获取失败");
        }

        return ResponseData.success(userBehavior);
    }
}

